package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.api.ApiService;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxObserver;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.generalplatformprojectclient.model.ResponseData;
import com.shqcjd.generalplatformprojectclient.model.pojo.DraftBean;
import com.shqcjd.generalplatformprojectclient.model.pojo.FileBean;
import com.shqcjd.generalplatformprojectclient.ui.adapter.EquipmentInfoPicAdapter;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.base.BasePresenter;
import com.shqcjd.generalplatformprojectclient.util.ToastUtils;
import com.shqcjd.generalplatformprojectclient.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class EquipmentInfoActivity extends BaseActivity {
    @BindView(R.id.btn_e_info_finish)
    Button btnEquipmentFinish;
    @BindView(R.id.tv_e_info)
    TextView tvEquipment;
    @BindView(R.id.tv_e_info_name)
    TextView tvEquipmentName;
    @BindView(R.id.tv_e_info_model)
    TextView tvEquipmentModel;
    @BindView(R.id.tv_e_info_specification)
    TextView tvEquipmentSpecification;
    @BindView(R.id.tv_e_info_manufacturer)
    TextView tvEquipmentManufacturer;
    @BindView(R.id.tv_e_info_info)
    TextView tvEquipmentInfo;
    @BindView(R.id.tv_e_info_repair)
    TextView tvEquipmentRepair;
    @BindView(R.id.tv_e_info_contactInformation)
    TextView tvEquipmentContactInformation;
    @BindView(R.id.tv_e_info_kks)
    TextView tvEquipmentKks;
    @BindView(R.id.tv_e_info_serialNumber)
    TextView tvEquipmentSerialNumber;
    @BindView(R.id.tv_e_info_manufacturingCode)
    TextView tvEquipmentManufacturingCode;
    @BindView(R.id.tv_e_info_custom_title0)
    TextView tvEquipmentCustomTitle0;
    @BindView(R.id.tv_e_info_custom_info0)
    TextView tvEquipmentCustomInfo0;
    @BindView(R.id.tv_e_info_custom_title1)
    TextView tvEquipmentCustomTitle1;
    @BindView(R.id.tv_e_info_custom_info1)
    TextView tvEquipmentCustomInfo1;
    @BindView(R.id.rv_e_info_pic)
    RecyclerView rvEquipmentPic;
    @BindView(R.id.tv_e_info_date)
    TextView tvEquipmentDate;
    @BindView(R.id.tv_e_info_file0)
    TextView tvEquipmentFile0;
    @BindView(R.id.tv_e_info_file1)
    TextView tvEquipmentFile1;
    @BindView(R.id.ll_selective_fill)
    LinearLayout llSelectiveFill;
    @BindView(R.id.rl_kks)
    RelativeLayout rlKks;
    @BindView(R.id.rl_serialNumber)
    RelativeLayout rlSerialNumber;
    @BindView(R.id.rl_manufacturingCode)
    RelativeLayout rlManufacturingCode;
    @BindView(R.id.ll_custom)
    LinearLayout llCustom;
    @BindView(R.id.rl_custom0)
    RelativeLayout rlCustom0;
    @BindView(R.id.rl_custom1)
    RelativeLayout rlCustom1;



    private Context context;
    private int eId;  //设备id
    private int type;  //1是设备  2是设备类型
    private EquipmentInfoPicAdapter selectPicAdapter;
    private List<FileBean> adapterList;
    private DraftBean mData;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_equipment_info;
    }

    @Override
    public void initView() {
        super.initView();
        btnEquipmentFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvEquipmentFile0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mData != null&&mData.descriptionFile!=null) {
                    Intent intent = new Intent(context, UIUtils.previewClass(UIUtils.getFormatName(mData.descriptionFile.filename)));
                    intent.putExtra("url", mData.descriptionFile.url);
                    intent.putExtra("name", mData.descriptionFile.filename);
                    startActivity(intent);
                }
            }
        });

        tvEquipmentFile1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mData != null&&mData.repairInstructionsFile!=null) {
                    Intent intent = new Intent(context, UIUtils.previewClass(UIUtils.getFormatName(mData.repairInstructionsFile.filename)));
                    intent.putExtra("url", mData.repairInstructionsFile.url);
                    intent.putExtra("name", mData.repairInstructionsFile.filename);
                    startActivity(intent);
                }
            }
        });

        if (type == 1) {
            tvEquipment.setText("设备详情");
        } else {
            tvEquipment.setText("设备类型详情");
        }
        updateEquimentInfo(eId);
    }

    @Override
    public void init() {
        super.init();
        context = this;
        eId = getIntent().getIntExtra("eId", 0);
        type = getIntent().getIntExtra("type", 0);
    }

    /**
     * 编辑设备信息
     */
    public void updateEquimentInfo(int id) {
        ApiService.equimentListById(id)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<DraftBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        showWaitingDialog("加载中...");
                    }

                    @Override
                    public void _onNext(ResponseData<DraftBean> draftBeanResponseData) {
                        if (draftBeanResponseData.code == 0) {
                            mData = draftBeanResponseData.data;
                            stvViewData(draftBeanResponseData.data);
                        } else {
                            ToastUtils.showShort(context, draftBeanResponseData.message);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    private void stvViewData(DraftBean data) {
        tvEquipmentName.setText(data.name);
        adapterList = new ArrayList<>();
        for (int i = 0; i < data.imageIdList.size(); i++) {
            FileBean f = new FileBean();
            f.filename = data.imageIdList.get(i).filename;
            f.id = data.imageIdList.get(i).id;
            f.url = data.imageIdList.get(i).url;
            adapterList.add(f);
        }
        selectPicAdapter = new EquipmentInfoPicAdapter(context, adapterList);
        rvEquipmentPic.setLayoutManager(new GridLayoutManager(context, 4));
        rvEquipmentPic.setAdapter(selectPicAdapter);
        selectPicAdapter.notifyDataSetChanged();

        tvEquipmentModel.setText(data.model);
        tvEquipmentSpecification.setText(data.specification);

        tvEquipmentDate.setText(data.productionDate == 0 ? "选择设备生产日期" : UIUtils.getTime(data.productionDate + ""));
        tvEquipmentManufacturer.setText(data.manufacturer);
        tvEquipmentInfo.setText(data.description);
        tvEquipmentContactInformation.setText(data.contactInformation);
        tvEquipmentRepair.setText(data.repairInstructions);

        if(data.descriptionFile == null){
            tvEquipmentFile0.setVisibility(View.INVISIBLE);
        }else{
            tvEquipmentFile0.setVisibility(View.VISIBLE);
            tvEquipmentFile0.setText(data.descriptionFile.filename);
        }

        if(data.repairInstructionsFile==null){
            tvEquipmentFile1.setVisibility(View.INVISIBLE);
        }else{
            tvEquipmentFile1.setVisibility(View.VISIBLE);
            tvEquipmentFile1.setText(data.repairInstructionsFile.filename);
        }

        showOrHideSelectiveFillInfo(data);
        showOrHideCustomInfo(data);
    }

    /**
     * 自定义信息显示或者隐藏
     */
    private void showOrHideCustomInfo(DraftBean data){
        if(isCustomInfoNull(data.addonVOList)){
            llCustom.setVisibility(View.GONE);
        }else{
            if(data.addonVOList.size() > 0){
                if(data.addonVOList.get(0).columnName.equals("")&&data.addonVOList.get(0).columnValue.equals("")){
                    rlCustom0.setVisibility(View.GONE);
                }else{
                    tvEquipmentCustomTitle0.setText(data.addonVOList.get(0).columnName);
                    tvEquipmentCustomInfo0.setText(data.addonVOList.get(0).columnValue);
                }
            }

            if(data.addonVOList.size() > 1){
                if(data.addonVOList.get(1).columnName.equals("")&&data.addonVOList.get(1).columnValue.equals("")){
                    rlCustom1.setVisibility(View.GONE);
                }else{
                    tvEquipmentCustomTitle1.setText(data.addonVOList.get(1).columnName);
                    tvEquipmentCustomInfo1.setText(data.addonVOList.get(1).columnValue);
                }
            }else{
                rlCustom1.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 判断自定义信息里面的是否为空
     * @return
     */
    private boolean isCustomInfoNull(List<DraftBean.AddonVOListBean> mList){
        for(int i=0; i<mList.size(); i++){
            if(!mList.get(i).columnName.equals("")||!mList.get(i).columnValue.equals("")){
                return false;
            }
        }
        return true;
    }

    /**
     * 选填信息显示或者隐藏
     */
    private void showOrHideSelectiveFillInfo(DraftBean data) {
        if (data.kks.equals("") && data.serialNumber.equals("") && data.manufacturingCode.equals("")) {
            llSelectiveFill.setVisibility(View.GONE);
        } else {
            if(data.kks.equals("")){
                rlKks.setVisibility(View.GONE);
            }else{
                tvEquipmentKks.setText(data.kks);
            }
            if(data.serialNumber.equals("")){
                rlSerialNumber.setVisibility(View.GONE);
            }else{
                tvEquipmentSerialNumber.setText(data.serialNumber);
            }
            if(data.manufacturingCode.equals("")){
                rlManufacturingCode.setVisibility(View.GONE);
            }else{
                tvEquipmentManufacturingCode.setText(data.manufacturingCode);
            }
        }
    }
}
